package com.molyfun.parents.fragment.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.SplashActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.utils.ActivityManager;
import com.molyfun.parents.utils.PreferencesStorageUtil;
import com.molyfun.parents.utils.UIUtils;
import com.molyfun.parents.view.common.MfEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class OrigiationEnterFragment extends DialogFragment implements View.OnClickListener {

    private static final String FLAG_RESTART = "need_restart";

    @InjectView(R.id.organization_invite_code)
    MfEditText mOrigiationInviteCode;
    @InjectView(R.id.cancel)
    TextView mCancel;
    @InjectView(R.id.commit)
    TextView mCommit;

    private boolean mNeedRestart = true;

    private static OrigiationEnterFragment newInstance(boolean needRestart) {
        OrigiationEnterFragment mStudentMenuDialogFragment = new OrigiationEnterFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(FLAG_RESTART, needRestart);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCancelable(false);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        mNeedRestart = getArguments().getBoolean(FLAG_RESTART, true);
        View view = inflater.inflate(R.layout.dialog_origiation_enter, container, false);
        ButterKnife.inject(this, view);
        mCancel.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                getDialog().dismiss();
                break;
            case R.id.commit:
                if (TextUtils.isEmpty(mOrigiationInviteCode.getText().toString().trim())) {
                    UIUtils.showToast(getContext(), "请输入机构邀请码");
                }
                enterOrganization(mOrigiationInviteCode.getText().toString().trim());
                break;
            default:
                break;
        }

    }



    private void enterOrganization(String organizationCode) {
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().bindOrganization(organizationCode, "stdnt",null,
                null, null, null);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if (response.body() != null) {
                    if(response.body().getCode() == 200){
                        if(getContext() != null){
                            UIUtils.showToast(getContext(), "申请成功");
                            if(mNeedRestart){
                                Intent intent = new Intent(MFPApplication.getInstance(), SplashActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                MFPApplication.getInstance().startActivity(intent);
                                PreferencesStorageUtil.setOriganOutShowSplash(MFPApplication.getInstance(), true);
                                ActivityManager.getInstance().finishMainActivity();

//                                SplashActivity.startActivity(getContext());
                                getActivity().finish();
                            }
                        }
                    }else{
                        UIUtils.showToast(getContext(), response.body().getMsg());
                    }
                }else{
                    UIUtils.showToast(getContext(), "申请出错");
                }
                if(getDialog() != null){
                    getDialog().dismiss();
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                UIUtils.showToast(getContext(), "申请出错");
                if(getDialog() != null){
                    getDialog().dismiss();
                }
            }
        });
    }

    public static class OrigiationEnterDialogBuilder {
        OrigiationEnterFragment mCommonDialogFragment;
        private String mTag;

        public OrigiationEnterDialogBuilder() {
            mCommonDialogFragment = OrigiationEnterFragment.newInstance(true);
            mTag = "origiation_enter";
        }

        public OrigiationEnterDialogBuilder(boolean needRestart) {
            mCommonDialogFragment = OrigiationEnterFragment.newInstance(needRestart);
            mTag = "origiation_enter";
        }

        public OrigiationEnterDialogBuilder setTag(String tag) {
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction) {
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
